﻿$(function () {
    $("#dictionary-grid").treegrid({
        url: $.Api("Sys/Dictionary/Grid"),
        nowrap: false, rownumbers: true, border: false,
        fit: true, singleSelect: true,
        idField: "ID", toolbar: "#dictionary-tools",
        treeField: "Name", parentField: "Parent",
        columns:
        [[
            { field: "Name", title: "名称", width: 200, align: "left" },
            { field: "Tag", title: "标记", width: 120, align: "center" },
            { field: "Remark", title: "描述", width: 280, align: "center" },
            { field: "IntSort", title: "排序", width: 50, align: "center" },
            {
                field: "IsVisible", title: "启用", width: 50, align: "center",
                formatter: function (v, r, i) { return $.GetYesOrNo(v); }
            }          
        ]]
    });
});

var Dictionary = {
    //添加
    Insert: function () {
        var dic = $("#dictype-grid").datagrid("getSelected");
        if (dic) {
            var row = $("#dictionary-grid").treegrid("getSelected");
            if (row) {
                Dictionary.Mutual("添加", "sys/dictionary/Edit.aspx?dic=" + dic.ID + "&pid=" + row.ID);
            }
            else { Dictionary.Mutual("添加", "sys/dictionary/Edit.aspx?dic=" + dic.ID); }
        }
        else { $.EasyuiSlide("请先选择数据词典!"); }
    },
    //修改
    Update: function () {
        var row = $("#dictionary-grid").treegrid("getSelected");
        if (row) {
            Dictionary.Mutual("修改", "sys/dictionary/Edit.aspx?id=" + row.ID);
        }
        else { $.EasyuiSlide(nullrowmsg); }
    },
    //删除
    Delete: function () {
        var row = $("#dictionary-grid").treegrid("getSelected");
        if (row) {
            $.EasyuiConfirm("确认删除?", function (del) {
                if (del) {
                    $.AjaxMask({
                        url: $.Api("Sys/Dictionary/Delete"),
                        type: "post", data: { id: row.ID },
                        success: function (result) {
                            result = JSON.parse(result);
                            $.EasyuiSlide(result.Message);
                            if (result.Success) { Dictionary.Reload(); }
                        }
                    });
                }
            });
        }
        else { $.EasyuiSlide(delnullrowmsg); }
    },
    //实体操作
    Mutual: function (title, href) {
        $.ModelDialog({
            title: title, href: href, width: 260,
            handler: function () {
                $.ModelForm({
                    validate: true, button: $(this),
                    id: "#dictionary-model",
                    url: $.Api("Sys/Dictionary/Save"),
                    success: function (result) { Dictionary.Reload(); }
                });
            },
            onLoad: function () { Dictionary.FormInit(); }
        });
    },
    //弹窗初始化
    FormInit: function () {
        var dic = $("#dictionary-dictypeid").val();
        $("#dictionary-parent").combotree({
            width: 150, url: $.Api("Sys/Dictionary/ComboTree&dic=" + dic),
            onLoadSuccess: function (node, data) {
                var nodeid = $("#dictionary-id").val();
                if (nodeid) {
                    var etree = $("#dictionary-parent").combotree("tree");
                    etree.tree("remove", etree.tree('find', nodeid).target);
                }
            }
        });
    },

    //排序向上
    Up: function () {
        Dictionary.Order("up");
    },
    //排序向下
    Down: function () {
        Dictionary.Order("down");
    },
    //排序操作
    Order: function (order) {
        var row = $("#dictionary-grid").treegrid("getSelected");
        if (row) {
            $.AjaxMask({
                url: $.Api("Sys/Dictionary/SetOrder"), type: "post",
                data: { id: row.ID, parent: row.Parent, dic: row.DicTypeID, order: order },
                success: function (result) {
                    result = JSON.parse(result);
                    $.EasyuiSlide(result.Message);
                    if (result.Success) { Dictionary.Reload(); }
                }
            });
        }
        else { $.EasyuiSlide(nullrowmsg); }
    },

    //启用
    Enable: function () {
        Dictionary.EnableOrDisable(true);
    },
    //禁用
    Disable: function () {
        Dictionary.EnableOrDisable(false);
    },
    //启用禁用
    EnableOrDisable: function (bool) {
        var row = $("#dictionary-grid").treegrid("getSelected");
        if (row) {
            $.AjaxMask({
                url: $.Api("Sys/Dictionary/EnableOrDisable"),
                type: "post", data: { id: row.ID, bit: bool },
                success: function (result) {
                    result = JSON.parse(result);
                    $.EasyuiSlide(result.Message);
                    if (result.Success) { Dictionary.Reload(); }
                }
            });
        }
        else { $.EasyuiSlide(nullrowmsg); }
    },

    //刷新列表
    Reload: function () {
        $.TreeGridReload("#dictionary-grid");
    }
};